package jobqueue;


import java.util.Vector;

public class Scheduler implements Runnable{
	
	private int sleeptime = 1000 * 5;
	private JobQueueImpl jobqueue;
	
	public Scheduler(JobQueueImpl j)
	{
		this.jobqueue = j;
	}
	
	public int getSleeptime() { return sleeptime; }
	public void setSleeptime(int s) { this.sleeptime = s;}
	
	@Override
	public void run() {
		while(true)
		{
			if(jobqueue.isEmpty())
			{
				sleep();
				continue;
			}
			
			Vector<JobQueueInfo> jobs = jobqueue.RemoveSchedulable();
			for(JobQueueInfo j : jobs)
			{
				j.submitJob();
			}
			sleep();
		}
	}
	
	private void sleep() {
		try{
			Thread.sleep(sleeptime);
		}catch(Exception e){e.printStackTrace();}
	}
	
}
